Python Conversion CheatSheet

Python default convertions


In [17]:
int_number = 1234

hex_string = hex(int_number)
print("hex(int_number) = {}".format(hex_string))

int_number = int(hex_string,16)
print("int(hex_string,16) = {}".format(int_number))

# 2 digit hex string
hex_string = "0x%0.2X" % 255 # = 0xFF
print("'0x%0.2X' % 255 = {}".format(hex_string))

hex_string = "%0.2X" % 255   # = FF
print("'%0.2X' % 255 = {}".format(hex_string))


hex(int_number) = 0x4d2
int(hex_string,16) = 1234
'0x%0.2X' % 255 = 0xFF
'%0.2X' % 255 = FF

Format Mini Language

format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  <any character>
align       ::=  "<" | ">" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
       "{" [field_name] ["!" conversion] [":" format_spec] "}"
          /                  "r"|"s"                   \
         /               (r)epr   (s)tr                 \
arg_name                                                 \
| ("." attribute_name | "[" element_index "]")*           \
|        |                       |                         \
|     identifier         integer | index_string            |
|                                   (quotes                |
[identifier                          not required)         |
 |integer]                                                 |
                                                           |
 _________________________________________________________/ \________
/                                                                    \
      ":"
         [[fill]align][sign][#][0][width][,][.precision][type]
  [default]--> < left    +   |  |  (int)       (int)    b base 2
  [default --> > right  [-]  |  |                       c character
   for         ^ center " "  |  \                       d base 10
   numbers]    =             |   `zero padding          e exponent (e)
                             |                          E exponent (E)
                            use 0b,0o,0x                f fixed point
                             for 2  8 16                F ^^(same)^^
  b base 2     c character                 [default]--> g general (???)
  o base 8     s string                                 G general 2 (?)
  d base 10                                             n number (general 3)
  x base 16                                             o base 8
  X base 16                                             s string
  e, E    exponent                         (lower case) x base 16
  f, F, % fixed point                      (upper case) X base 16
  g, G, n (general numbers)                   (x100, f) % percentage

In [49]:
var = 1234.56789
print("{:>6.5}".format(var))        # right aligned to 6 digits, precision 5 (5 numbers including point e.g. 0.123)
     
print("{:020}".format(var))         # filled up with 0 to 20 digits, 00000000001234.56789

print('{:{width}.{prec}f}'.format(var, width=5, prec=2))

print('{:_<10}'.format('test'))
print('{:_>10}'.format('test'))
print('{:_^10}'.format('test'))
     
  
print("0x{:0x}".format(int(var)))   # in hex small case, 0x4d2
print("0x{:0X}".format(int(var)))   # in hex small case, 0x4D2
     
print("{:04x}".format(int(var)))    # in hex small case, 0x04d2
print("{:04X}".format(int(var)))    # in hex small case, 0x04D2


1234.6
00000000001234.56789
1234.57
test______
______test
___test___
0x4d2
0x4D2
04d2
04D2

Binascii


In [9]:
import binascii

hex_string = "FEED"
hex_bin = b'\xfe\xed'
conv_hex_bin = binascii.unhexlify(hex_string)
print("binascii.unhexlify(hex_string) = {}".format(conv_hex_bin))

conv_hex_string = binascii.hexlify(hex_bin)
print("binascii.hexlify(hex_bin) = {}".format(conv_hex_string))


binascii.unhexlify(hex_string) = b'\xfe\xed'
binascii.hexlify(hex_bin) = b'feed'

My hex2bin()


In [12]:
def hex2bin(str):
    bin = ['0000','0001','0010','0011',
           '0100','0101','0110','0111',
           '1000','1001','1010','1011',
           '1100','1101','1110','1111']
    aa = ''
    for i in range(len(str)):
        aa += bin[int(str[i],base=16)]
    return aa
# END hex2bin
hex_string = "FEED"
bin_string = hex2bin(hex_string)
print("hex2bin(hex_string) = {}".format(bin_string))


hex2bin(hex_string) = 1111111011101101